package com.wonders.frame.service;

import com.wonders.frame.entity.HuiAreas;
import com.wonders.frame.entity.Tree.AreasNodeData;
import com.wonders.frame.repository.HuiAreasRepository;
import com.wonders.frame.repository.core.CoreRepository;
import com.wonders.frame.service.Core.CoreCurdService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class HuiAreasService extends CoreCurdService<HuiAreas> {

    @Autowired
    private HuiAreasRepository repository;

    @Override
    protected CoreRepository<HuiAreas> getRepository() {
        return repository;
    }

//    public List<AreasNodeData> getAreasTree(){
//        List<AreasNodeData> nodes = new ArrayList<AreasNodeData>();
//        List<HuiAreas> areas = repository.findAll();
//        for(HuiAreas area:areas){
//            if(area.getParentDomainId().equals("0")){
//                nodes.add(getAreasNode(area));
//            }
//        }
//        return nodes;
//    }

    //    public AreasNodeData getAreasNode(HuiAreas huiAreas){
//        AreasNodeData areasNodeData = new AreasNodeData();
//        areasNodeData.setValue(huiAreas.getDomain());//值存放行政机构代码
//        areasNodeData.setLabel(huiAreas.getName());
//        if(huiAreas.getAreaChildren()!=null ){
//            <AreasNodeData> nodes = new ArrayList<AreasNodeData>();
//            for(HuiAreas area :huiAreas.getAreaChildren()){
//                nodes.add(getAreasNode(area));
//            }
//            areasNodeData.setChildren(nodes);
//        }
//        return areasNodeData;
//
//    }


    public List<AreasNodeData> getAreasTree(){
        List<AreasNodeData> nodes = new ArrayList<AreasNodeData>();
        List<HuiAreas> areas = repository.findAllOrderBySeq();
        for(HuiAreas area1:areas){
            if(Strings.isNotEmpty(area1.getpId()) && area1.getpId().equals("0")){
                //省级
                AreasNodeData areasNodeData = new AreasNodeData();
                areasNodeData.setValue(area1.getDomain());//值存放行政机构代码
                areasNodeData.setLabel(area1.getName());
                List<AreasNodeData> node1 = new ArrayList<AreasNodeData>();
                for(HuiAreas area2:areas){
                    //市级
                    if(Strings.isNotEmpty(area2.getpId()) && area1.getId().equals(area2.getpId())){
                        AreasNodeData areasNodeData2 = new AreasNodeData();
                        areasNodeData2.setValue(area2.getDomain());//值存放行政机构代码
                        areasNodeData2.setLabel(area2.getName());
                        List<AreasNodeData> node2 = new ArrayList<AreasNodeData>();
                        for(HuiAreas area3:areas){
                            //县级
                            if(Strings.isNotEmpty(area3.getpId()) && area2.getId().equals(area3.getpId())){
                                AreasNodeData areasNodeData3 = new AreasNodeData();
                                areasNodeData3.setValue(area3.getDomain());//值存放行政机构代码
                                areasNodeData3.setLabel(area3.getName());
                                areasNodeData3.setChildren(null);
                                node2.add(areasNodeData3);
                            }
                        }
                        areasNodeData2.setChildren(node2);
                        node1.add(areasNodeData2);
                    }
                }
                areasNodeData.setChildren(node1);
                nodes.add(areasNodeData);
            }
        }
        return nodes;
    }


}
